package net.jarang.study.spring.web;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import net.jarang.study.spring.annotation.User;
import net.jarang.study.spring.board.VisitBO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ViewController {	
	@Autowired VisitBO visitBO;
	
	@RequestMapping("/view1")
	public ModelAndView view1(String userId) {
		User user = visitBO.selectUser(userId);
		ModelAndView mav = new ModelAndView("view1");
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping("/view2")
	public ModelAndView view2(@Valid User user, BindingResult bindingResult) {
		ModelAndView mav = new ModelAndView("view2");
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping("/view3")
	public ModelAndView view3(@Valid User user, BindingResult bindingResult) {
		ModelAndView mav = new ModelAndView("view3");
		user.setVisitCount(200);
		user.setRegistered(true);
		
		Map<String, String> interests = new HashMap<String, String>();
		interests.put("A", "java");
		interests.put("B", "c");
		interests.put("C", "python");
		
		mav.addObject("user", user);
		mav.addObject("interests", interests);
		return mav;
	}
}
